<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏点击缩放事件 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 边栏菜单区 unique-opened:只有一个子菜单被展开 collapse:展开收起状态 collapse-transition:动画效果取消 router:路由开启 default-active:高亮菜单-->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级带单 key:唯一标识-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 根据id绑定不同图标-->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 标签内容 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 index:路由跳转路径与高亮标识 key:唯一标识 saveNavState:点击事件 保存连接和高亮标识 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {

      },
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 获取保存在浏览器的激活地址
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 清空浏览器中token
      window.sessionStorage.clear()
      // 重定向到 登陆页
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const {
        data: res
      } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮触发菜单折叠展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      // 点击左侧菜单时 将连接保存至浏览器
      window.sessionStorage.setItem('activePath', activePath)
      // 被激活的连接地址 更换
      this.activePath = activePath
    }
  }
}

</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  // 左右分离
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  // 上下位置居中
  align-items: center;
  color: #fff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  // 文本居中
  text-align: center;
  // 文本间距
  letter-spacing: 0.2em;
  // 鼠标悬停样式
  cursor: pointer;
}
</style>
